﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mitasoft.ERS.Web.Utils;

namespace mitasoft.ERS.Web
{
    public partial class IssuePage : System.Web.UI.Page
    {
        private int currentIssueID = -1;

        private Business.VW_IssueDetail currentIssue;
        public Business.VW_IssueDetail CurrentIssue
        {
            get
            {
                return currentIssue;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();

            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            CheckQueryStringIssueID();
        }

        private void BindData()
        {
            BindIssuesStatus();
            BindCommentsList();
        }
        private void BindIssuesStatus()
        {
            ddlIssueStatus.DataSource = Utils.CacheFactory.GetIssueStatusList;
            ddlIssueStatus.DataBind();
        }
        private void BindCommentsList()
        {
            lstComments.DataSource = Comment.GetData();
            lstComments.DataBind();
        }
        private void CheckQueryStringIssueID()
        {
            if (Page.Request.QueryString["id"] != null)
            {
                try
                {
                    currentIssueID = Convert.ToInt32(Page.Request.QueryString["id"]);
                    PresentIssue(currentIssueID);
                }
                catch (OverflowException)
                {
                    currentIssueID = -1;
                    //TODO: Write to log
                }
                catch (Exception)
                {
                    currentIssueID = -1;
                    //TODO: Write to log
                }
            }
        }
        private void PresentIssue(int issueID)
        {
            using (Business.ERSEntities ctx = new Business.ERSEntities())
            {
                currentIssue = ctx.GetIssueDetail(issueID);
            }

            issueHeader.Title = string.Format("ID: #{0}", CurrentIssue.IDIssue);
            issueHeader.Description = string.Format("Issue status: {0}", CurrentIssue.StatusDescription);
            issueHeader.IssueType = GetStatusType(CurrentIssue.StatusDescription);

            issueTitle.Text = CurrentIssue.Title;
            issueProject.Text = CurrentIssue.ProjectName;
            issueType.Text = CurrentIssue.TypeDescription;
            issueDescription.InnerHtml = HttpContext.Current.Server.HtmlDecode(CurrentIssue.IssueDescription);
            issueStatus.Text = CurrentIssue.StatusDescription;

        }


        private controls.ImageTypeEnum GetStatusType(string type)
        {
            if (type.Equals("Open"))
                return controls.ImageTypeEnum.OpenIssue;
            else if (type.Equals("Closed"))
                return controls.ImageTypeEnum.ClosedIssue;
            else if (type.Equals("In progress"))
                return controls.ImageTypeEnum.OpenIssue;

            return controls.ImageTypeEnum.OpenIssue;
        }

    }

    public class Comment
    {
        public string Description { get; set; }
        public string User { get; set; }
        public DateTime AddedDate { get; set; }

        public static List<Comment> GetData()
        {
            return new List<Comment>()
            {
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(1), Description = "Ceva" + Environment.NewLine + "Altceva" + "Ceva" + Environment.NewLine + "Altceva" + "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(2), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(3), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(4), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(5), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(6), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(7), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(8), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(9), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(16), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(17), Description = "Ceva" + Environment.NewLine + "Altceva"},
                new Comment { User="mitasoft", AddedDate = DateTime.Now.AddHours(18), Description = "Ceva" + Environment.NewLine + "Altceva"},
            };
        }
    }
}